using System;
using System.Linq;
using HQLAddin.Highlighting;
using Microsoft.VisualStudio.Text;
using Moq;
using NUnit.Framework;
using SharpTestsEx;

namespace HQLAddin.Tests
{
	[TestFixture]
	public class SnapshotExtensionTests
	{
		[Test]
		public void GetQueryInLinePositionShouldWork()
		{
			var snapshot = CreateSnapshot(System.IO.File.ReadAllLines(@"examples\example1.xml"));

			var result = snapshot.GetQueryAtLine(14);
			result.Trim().Satisfy(r => r.StartsWith("from Foo") && r.EndsWith("f.id = 1"));
		}

		[Test]
		public void GetQueryNameInLineShouldWork()
		{
			var snapshot = CreateSnapshot(System.IO.File.ReadAllLines(@"examples\example1.xml"));

			snapshot.GetQueryNameAtLine(14).Should().Be.EqualTo("Bar");
			snapshot.GetQueryNameAtLine(13).Should().Be.EqualTo("Bar");

			snapshot.GetQueryNameAtLine(16).Should().Be.EqualTo("Foo");
			snapshot.GetQueryNameAtLine(17).Should().Be.EqualTo("Foo");
			snapshot.GetQueryNameAtLine(18).Should().Be.EqualTo("Foo");
		}

		private static ITextSnapshot CreateSnapshot(params string[] lines)
		{
			var snapshot = new Mock<ITextSnapshot>();
			snapshot.Setup(s => s.Lines)
				.Returns(lines.Select(CreateMockedLine));

			snapshot.Setup(sn => sn.LineCount).Returns(lines.Count());
			return snapshot.Object;
		}

		private static ITextSnapshotLine CreateMockedLine(string lineContent)
		{
			var line = new Mock<ITextSnapshotLine>();
			line.Setup(l => l.GetText()).Returns(lineContent);
			return line.Object;
		}
	}
}